<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteHistory">
        <span @click="$emit('deleteAllHistories')">全部删除</span>
        &nbsp;&nbsp;
        <span @click="isDeleteHistory = false">完成</span>
      </div>
      <van-icon v-else
        @click="isDeleteHistory = true"
        name="delete" />
    </van-cell>
    <van-cell :title="item"
      v-for="(item,index) in searchHistories "
      :key="index"
      @click="delCurrentHistory(item,index)">
      <van-icon v-show="isDeleteHistory"
        name="close" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isDeleteHistory: false
    }
  },
  methods: {
    delCurrentHistory (item, index) {
      if (this.isDeleteHistory) {
        // 删除状态，删除历史记录
        this.searchHistories.splice(index, 1)
      } else {
        // 非删除状态，把历史记录添加到搜索框
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
